<?php
    namespace common\models;

    use Yii;
    use yii\base\Model;
    use yii\web\UploadedFile;

    /**
     * UploadForm is the model behind the upload form.
     */
    class UploadForm extends Model
    {
        /**
         * @var UploadedFile file attribute
         */
        public $file;
        public $file1;
        public $file2;
        public $file3;

        public $dir;
        public $imgname;
        private $min = 1000000000;
        private $max = 9999999999;

        /**
         * @return array the validation rules.
         */
        public function rules()
        {
            return [
                [['file'], 'file', 'skipOnEmpty' => false],
            ];
        }

        /**
         * @inheritdoc
         */
        public function attributeLabels()
        {
            return [
                'file' => '',
            ];
        }

        public function upImage($model){
            $this->dir = date('Ymd', time());
            $path = Yii::$app->params['gaopinurl']. '\\' . $this->dir;
            if (!file_exists($path)) {
                mkdir("$path", 0777, true);
            }
//        echo Url::base(true);exit;
            $this->file = UploadedFile::getInstance($model, 'file');

            $this->imgname = date('YmdHms', time()) . mt_rand($this->min, $this->max);
            if ($this->file && $this->validate())
                $this->file->saveAs($path. '\\' . $this->imgname . '.' . $this->file->extension);
        }

        public function uploadImage($model,$path,$imgname,$extension=null){
                $this->file = UploadedFile::getInstance($model, $imgname);
                if ($this->file && $this->validate()){
                    $this->file->saveAs($path. '\\' . $imgname . '.' . $extension);
                    Yii::$app->getSession()->setFlash('success','可以啦');
                    return true;
                }
                else{
                    Yii::$app->getSession()->setFlash('error','失败了');
                }
                return false;
        }
    }

?>